package com.jeasonzhao.commons.utils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;

public class Guid
{

    private String m_strValueBeforeMD5 = "";
    private String m_strValueAfterMD5 = "";
    private static Random m_myRand = null;
    private static SecureRandom m_mySecureRand = null;

    private static String m_strSID = null;
    static
    {
        m_mySecureRand = new SecureRandom();
        long secureInitializer = m_mySecureRand.nextLong();
        m_myRand = new Random(secureInitializer);
        try
        {
            m_strSID = InetAddress.getLocalHost().toString();
        }
        catch(UnknownHostException e)
        {
            e.printStackTrace();
        }
    }

    private String getValueAfterMD5()
    {
        return m_strValueAfterMD5;
    }

    private Guid()
    {
        getRandomGUID(false);
    }

    private Guid(boolean secure)
    {
        getRandomGUID(secure);
    }

    private void getRandomGUID(boolean secure)
    {
        try
        {
            StringBuffer sbValueBeforeMD5 = new StringBuffer();
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            long time = System.currentTimeMillis();
            long rand = 0;

            if(secure)
            {
                rand = m_mySecureRand.nextLong();
            }
            else
            {
                rand = m_myRand.nextLong();
            }

            // This StringBuffer can be a long as you need; the MD5
            // hash will always return 128 bits.  You can change
            // the seed to include anything you want here.
            // You could even stream a file through the MD5 making
            // the odds of guessing it at least as great as that
            // of guessing the contents of the file!
            sbValueBeforeMD5.append(m_strSID);
            sbValueBeforeMD5.append(':');
            sbValueBeforeMD5.append(Long.toString(time));
            sbValueBeforeMD5.append(':');
            sbValueBeforeMD5.append(Long.toString(rand));

            m_strValueBeforeMD5 = sbValueBeforeMD5.toString();
            md5.update(m_strValueBeforeMD5.getBytes());

            byte[] array = md5.digest();
            StringBuffer sb = new StringBuffer();
            for(int j = 0;j < array.length;++j)
            {
                int b = array[j] & 0xFF;
                if(b < 0x10)
                {
                    sb.append('0');
                }
                sb.append(Integer.toHexString(b));
            }

            m_strValueAfterMD5 = sb.toString();

        }
        catch(NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
    }

    /*
     * Convert to the standard format for GUID
     * (Useful for SQL Server UniqueIdentifiers, etc.)
     * Example: C2FEEEAC-CFCD-11D1-8B05-00600806D9B6
     */
    public String toString()
    {
        String raw = m_strValueAfterMD5.toUpperCase();
        StringBuffer sb = new StringBuffer();
        sb.append(raw.substring(0,8));
        sb.append('-');
        sb.append(raw.substring(8,12));
        sb.append('-');
        sb.append(raw.substring(12,16));
        sb.append('-');
        sb.append(raw.substring(16,20));
        sb.append('-');
        sb.append(raw.substring(20));
        return sb.toString();
    }

    public static String newGuid(String strPrefix)
    {
        return(null == strPrefix || strPrefix.trim().length() < 1 ? "" : strPrefix.trim()) +
            (new Guid(true)).getValueAfterMD5();
    }

    public static String newGuid()
    {
        return(new Guid(true)).getValueAfterMD5();
    }
}
